﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace Tal.CaptchaControl
{
    /// <summary>
    /// Image generation code taken from http://www.codeproject.com/KB/aspnet/CaptchaImage.aspx
    /// </summary>
    public class GrayImageGenerator:ImageGenerator
    {
        public GrayImageGenerator(string imageText):base(imageText)
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public override System.Drawing.Bitmap GenerateImage()
        {
            // Create a new 32-bit bitmap image.

            Bitmap bitmap = new Bitmap(
              this.ImageWidth,
              this.ImageHeight,
              PixelFormat.Format32bppArgb);

            // Create a graphics object for drawing.

            Graphics g = Graphics.FromImage(bitmap);
            g.SmoothingMode = SmoothingMode.AntiAlias;
            Rectangle rect = new Rectangle(0, 0, this.ImageWidth, this.ImageHeight);

            // Fill in the background.

            HatchBrush hatchBrush = new HatchBrush(
              HatchStyle.SmallConfetti,
              Color.LightGray,
              Color.White);
            g.FillRectangle(hatchBrush, rect);

            // Set up the text font.

            SizeF size;
            float fontSize = rect.Height + 1;
            Font font;
            // Adjust the font size until the text fits within the image.

            do
            {
                fontSize--;
                font = new Font(
                  this.FontFamily,
                  fontSize,
                  FontStyle.Bold);
                size = g.MeasureString(this.ImageText, font);
            } while (size.Width > rect.Width);

            // Set up the text format.

            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;

            // Create a path using the text and warp it randomly.

            GraphicsPath path = new GraphicsPath();
            path.AddString(
              this.ImageText,
              font.FontFamily,
              (int)font.Style,
              font.Size, rect,
              format);
            float v = 4F;
            Random random = new Random();
            PointF[] points =
      {
        new PointF(
          random.Next(rect.Width) / v,
          random.Next(rect.Height) / v),
        new PointF(
          rect.Width - random.Next(rect.Width) / v,
          random.Next(rect.Height) / v),
        new PointF(
          random.Next(rect.Width) / v,
          rect.Height - random.Next(rect.Height) / v),
        new PointF(
          rect.Width - random.Next(rect.Width) / v,
          rect.Height - random.Next(rect.Height) / v)
      };
            Matrix matrix = new Matrix();
            matrix.Translate(0F, 0F);
            path.Warp(points, rect, matrix, WarpMode.Perspective, 0F);

            // Draw the text.

            hatchBrush = new HatchBrush(
              HatchStyle.LargeConfetti,
              Color.LightGray,
              Color.DarkGray);
            g.FillPath(hatchBrush, path);

            // Add some random noise.

            int m = Math.Max(rect.Width, rect.Height);
            for (int i = 0; i < (int)(rect.Width * rect.Height / 30F); i++)
            {
                int x = random.Next(rect.Width);
                int y = random.Next(rect.Height);
                int w = random.Next(m / 50);
                int h = random.Next(m / 50);
                g.FillEllipse(hatchBrush, x, y, w, h);
            }

            // Clean up.

            font.Dispose();
            hatchBrush.Dispose();
            g.Dispose();
            return bitmap;

        }
    }
}
